
<html>
<head><title>CS143 Project 1B</title></head>
<body>
<p>
This page was made by Masaki Moritani (303650525).
</p>
Type an SQL query in the following box:
<p>
<form method="GET">
<?php
print('<textarea name="query" cols="60" rows="8">'.$_GET["query"].'</textarea>');
?>
<input type="submit" value="Submit" />
</form>
</p>
<p><small>Note: tables and fields are case sensitive. Run "show tables" to see the list of
available tables.</small>
</p>

<?php
if ($_GET["query"] != "")
{
  print("<h3>Results from MySQL:</h3>");

  # Connect to MySQL server and select database
  $db_connection = mysql_connect("localhost", "cs143", "");
  if(!$db_connection)
  {
    $errmsg = mysql_error($db_connection);
    print("Connection to MySQL server failed: $errmsg <br />");
    exit(1);
  }
  mysql_select_db("CS143", $db_connection);

  # Issue user query and obtain results
  $rs = mysql_query($_GET["query"], $db_connection);
  
  # Get number of rows and number of columns
  $n_cols = mysql_num_fields($rs);

  # Show results in a table
  print("<table border=1 cellspacing=1 cellpadding=2>");
  print("<tr align=center>");
  for($i = 0; $i < $n_cols; $i++)
  {
    print("<td><b>".mysql_field_name($rs,$i)."</b></td>");
  }
  print("</tr>");
  while($row = mysql_fetch_row($rs))
  {
    print("<tr align=center>");
    foreach($row as $value)
    {
      if(is_null($value)) $value = 'N/A';
      print("<td>".$value."</td>");
    }
    print("</tr>");
  }
  print("</table>");

  # Disconnect from MySQL server
  mysql_close($db_connection);

}
?>

</body>
</html>

